#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_NetXApi/NetXApi_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_NetXApi/NetXApi_Error.h"

//g++ -std=gnu++11 -Wall -g Test_ForNetXApi.cpp -o Test_ForNetXApi -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core -lNetEngine_BaseLib -lNetEngine_NetXApi -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core,--disable-new-dtags

static void NetXApi_Sniffer_Callback(XNETHANDLE xhNet, NETXAPI_PROTOCOLINFO *pSt_ProtoInfo, LPCTSTR lpszMsgBuffer, LPVOID lParam)
{
    printf("Source:%s Dest:%s nHdrLen:%d nMsgLen:%d\n", pSt_ProtoInfo->tszSourceAddr, pSt_ProtoInfo->tszDestAddr, pSt_ProtoInfo->nHdrLen, pSt_ProtoInfo->nMsgLen);
    for (int i = 0; i < pSt_ProtoInfo->nMsgLen; i++)
    {
        printf("%02X ", (UCHAR)lpszMsgBuffer[pSt_ProtoInfo->nHdrLen + i]);
    }
    printf("\n\n");
}
static void NetXApi_Enum_Callback(XNETHANDLE xhNet, LPCTSTR lpszIPAddr, LPCTSTR lpszMacAddr, LPVOID lParam)
{
    printf("%llu %s %s\n", xhNet, lpszIPAddr, lpszMacAddr);
}
int Test_IPAddr()
{
    ///*IP库
    TCHAR tszLocal[256];
    TCHAR tszArea[256];

    if (!NetXApi_Address_OpenQQWry(_T("./NetEngine_QQWay.bin")))
    {
        return -1;
    }
    if (!NetXApi_Address_IPtoAddr(_T("222.209.29.131"), tszLocal, tszArea))
    {
        return -2;
    }
    _tprintf(_T("%s,%s\n"), tszLocal, tszArea);

    if (!NetXApi_Address_IPtoAddr(_T("8.8.8.8"), tszLocal, tszArea))
    {
        return -2;
    }
    _tprintf(_T("%s,%s\n"), tszLocal, tszArea);
    NetXApi_Address_CloseQQWry();

    return 0;
}
int Test_NetFlow()
{
    //网络流量
    NETXAPI_FLOWSTATE st_FlowState;
    memset(&st_FlowState, '\0', sizeof(NETXAPI_FLOWSTATE));

    NetXApi_NetFlow_GetAll(&st_FlowState, "Realtek PCIe GBE Family Controller");
    printf("Send:%llu Recv:%llu\n", st_FlowState.st_SendPackets.ullBytes, st_FlowState.st_RecvPackets.ullBytes);

    return 0;
}
int Test_NetSniffer()
{
    //网路嗅探
    list<NETXAPI_SNIFFERIF> stl_IFSniffer;
    NetXApi_Sniffer_GetIFAll(&stl_IFSniffer);
    list<NETXAPI_SNIFFERIF>::const_iterator stl_ListIterator = stl_IFSniffer.begin();
    for (; stl_ListIterator != stl_IFSniffer.end(); stl_ListIterator++)
    {
        printf("%s:%s\n", stl_ListIterator->tszIFName, stl_ListIterator->tszIFDes);
    }
    stl_IFSniffer.clear();

    XNETHANDLE xhNet;
    NetXApi_Sniffer_Start(&xhNet, _T("ens33"), NetXApi_Sniffer_Callback);

    return 0;
}
int Test_NetEnum()
{
    XNETHANDLE xhNet;
    LPCTSTR lpszStartAddr = _T("192.168.1.1");
    LPCTSTR lpszEndAddr = _T("192.168.1.10");

    NetXApi_LANEnum_Start(&xhNet, _T("ens33"), lpszStartAddr, lpszEndAddr, NetXApi_Enum_Callback);
    Sleep(5000);
    NetXApi_LANEnum_Close(xhNet);
    return 0;
}
int Test_CtrlFlow()
{
    XNETHANDLE xhNet;
    XNETHANDLE xhFilter;
    NetXApi_CtrlFlow_Init(&xhNet,"eth0");
    NetXApi_CtrlFlow_AddFlow(xhNet, &xhFilter, 1000000, 10000, 5000);

    for (int i = 0;i < 30;i++)
    {
        Sleep(1000);
    }
    NetXApi_CtrlFlow_DelFlow(xhNet, xhFilter);
    NetXApi_CtrlFlow_Destory(xhNet);

    return 0;
}
int main()
{
    Test_IPAddr();
    Test_NetEnum();
    Test_NetSniffer();

    while (1)
    {
        Sleep(1000);
    }
    return 0;
}
